/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ar.lucas.controlgasto.statgraphs;

import ar.lucas.controlgasto.resources.Consultas;
import ar.lucas.controlgasto.utils.Validations;
import java.awt.Color;
import java.awt.GradientPaint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author lucmussi
 */
public class drawBarGraphs {

//    public class BarChartDemo {
//
////    public BarChartDemo(final String title) {
////
////        super(title);
////
////        final CategoryDataset dataset = createDataset();
////        final JFreeChart chart = createChart(dataset);
////        final ChartPanel chartPanel = new ChartPanel(chart);
////        chartPanel.setPreferredSize(new Dimension(500, 270));
////        setContentPane(chartPanel);
////
////    }

    public ChartPanel genBarGraph(Integer id_persona, Integer primerMes, Integer primerAnio, Integer segundoMes, Integer segundoAnio){
        CategoryDataset bardata = createDataset(id_persona,primerMes,primerAnio,segundoMes,segundoAnio);
        JFreeChart chart = createChart(bardata);
        ChartPanel panel = new ChartPanel(chart);
        return panel;
    }

    private CategoryDataset createDataset(Integer id_persona, Integer primerMes, Integer primerAnio, Integer segundoMes, Integer segundoAnio){
        Double primero = Consultas.buscarTotalMes(id_persona, primerMes, primerAnio);
        Double segundo = Consultas.buscarTotalMes(id_persona, segundoMes, segundoAnio);
        String nombreMes1 = Validations.getMonthForInt(primerMes-1);
        String nombreMes2 = Validations.getMonthForInt(segundoMes-1);
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(primero, nombreMes1, primerAnio);
        dataset.addValue(segundo, nombreMes2, segundoAnio);
        return dataset;
    }

    private JFreeChart createChart(final CategoryDataset dataset) {

        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(
            "Comparación",         // chart title
            "Año",               // domain axis label
            "Monto",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);

        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue,
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green,
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red,
            0.0f, 0.0f, Color.lightGray
        );
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);

        final CategoryAxis domainAxis = plot.getDomainAxis();
        /*domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / .0)
        );*/
        // OPTIONAL CUSTOMISATION COMPLETED.

        return chart;

    }

    }

